package com.tjhk.netty.util;

import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Netty ByteBuf工具类
 */
public class ByteBufUtils {
    private static final Logger log = LoggerFactory.getLogger(ByteBufUtils.class.getSimpleName());

    /**
     * 读取固定长度并返回
     * @param input
     * @param length
     * @return
     */
    public static byte[] readFixLength(ByteBuf input, int length) {
        if (input.readableBytes() < length){
            length = input.readableBytes();
            log.warn("读取长度大于数据长度！");
        }
        byte[] bytes = new byte[length];
        input.readBytes(bytes);
        return bytes;
    }
}